package com.meixiang.activity.account.managers.myPurse;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.activity.account.MemberCardActivity;
import com.meixiang.activity.homes.shopping.CashierDeskActivity;
import com.meixiang.entity.OrderResult;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.tool.Tool;
import com.meixiang.view.CheckEditTextEmptyButton;
import com.meixiang.view.ClearEditText;
import com.meixiang.view.TitleView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的钱包 充值
 *
 * @author zys
 */
public class RechargeActivity extends BaseActivity {

    @Bind(R.id.title)
    TitleView title;
    @Bind(R.id.et_recharge_number)
    ClearEditText etRechargeNumber;
    @Bind(R.id.tv_recharge)
    CheckEditTextEmptyButton tvRecharge;
    private String rechargeType;
    public static final String RECHARGE_TYPE = "rechargeType";
    public static final String PACKAGE_RECHARGE = "1"; // 钱包充值
    public static final String CLUB_CARD_RECHARGE = "2"; // 会员卡充值

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_recharge);
        ButterKnife.bind(this);
        if (getIntent() != null) {
            rechargeType = getIntent().getStringExtra(RECHARGE_TYPE);
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        title.setTitle("余额充值");
        title.showRightButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
        title.setLeftImageButton(R.mipmap.back);
        title.showLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void setData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }


    @OnClick(R.id.tv_recharge)
    public void onClick(View v) {
        getRecharge();
    }

    private void getRecharge() {
        String amount = etRechargeNumber.getText().toString();
        if (!Tool.isAmountText(amount) || Float.valueOf(amount) < 0.01f) {
            Toast.makeText(this, "请输入正确的金额", Toast.LENGTH_LONG).show();
            return;
        }
        HttpParams params = new HttpParams();
        params.put("rechargeType", rechargeType);
        params.put("amount", amount);
        HttpUtils.post(Config.RECAHRAGE_URL, params, new HttpCallBack(RechargeActivity.this) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                try {
                    String orderId = result.getString("orderId");
                    String amount = result.getString("orderAmount");
                    Intent intent = new Intent(context, CashierDeskActivity.class);
                    intent.putExtra(CashierDeskActivity.ORDER_RESULT, new OrderResult(amount, orderId));
                    HashMap<String, Class> map = new HashMap<>();
                    if (rechargeType.equals(PACKAGE_RECHARGE)) {
                        // 钱包充值
                        intent.putExtra(CashierDeskActivity.PAYMENT_TYPE, CashierDeskActivity.PACKAGE_PAY);
                        map.put(CashierDeskActivity.GOTO_ACTIVITY, MyPurseActivity.class);
                        intent.putExtra(CashierDeskActivity.GOTO_ACTIVITY, map); // 支付成功之后跳转的activity
                    } else {
                        // 会员卡充值
                        intent.putExtra(CashierDeskActivity.PAYMENT_TYPE, CashierDeskActivity.CLUB_CARD_PAY);
                        map.put(CashierDeskActivity.GOTO_ACTIVITY, MemberCardActivity.class);
                        intent.putExtra(CashierDeskActivity.GOTO_ACTIVITY, map); // 支付成功之后跳转的activity
                    }
                    startActivity(intent);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String code, String message) {
                Toast.makeText(RechargeActivity.this, message, Toast.LENGTH_LONG).show();
            }
        });
    }

}
